/*
 ***************************************************************************
 * MediaTeK Inc.
 * 4F, No. 2 Technology	5th	Rd.
 * Science-based Industrial	Park
 * Hsin-chu, Taiwan, R.O.C.
 *
 * (c) Copyright 2002-2012, MTK.
 *
 * All rights reserved.	MediaTeK's source	code is	an unpublished work	and	the
 * use of a	copyright notice does not imply	otherwise. This	source code
 * contains	confidential trade secret material of MediaTeK Tech. Any attemp
 * or participation	in deciphering,	decoding, reverse engineering or in	any
 * way altering	the	source code	is stricitly prohibited, unless	the	prior
 * written consent of MediaTeK, Inc. is obtained.
 ***************************************************************************

	Module Name:
	omci_utily.c

	Abstract:

	Revision History:
	Who			When			What
	--------	----------		----------------------------------------------
	Name		Date			Modification logs
	Andy.Yi		2012/7/3	Create
*/

#ifndef _GENERAL_FUNC_H_
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "omci.h"
#include "omci_utily.h"
#include "omci_me_external.h"


/****************************************************************************/

#define OMCI_UPLOAD_FW "/tmp/omci_fw"
// It is truly a disaster to define  these addresses(offset) randomly
// Always a good habit to define them in the same  place, for easy understanding and less opportunity to mess up with the flash layout


#define DOWNLOAD_SEC_AR		(1<<6)
#define RESERVEAREA_BLOCK_BASE 0
#define SOFTWARE_RESTART 1

#define MAX_ATTRIBUTE_NUM 			16
#define SET_MAX_CONTENT_LEN  		30
#define GET_MAX_CONTENT_LEN 25
#define GET_NEXT_MAX_CONTENT_LEN_EXT 1963
#define SET_MAX_CONTENT_LEN_EXT 	1964
#define GET_MAX_CONTENT_LEN_EXT 	1959
#define GET_NEXT_MAX_CONTENT_LEN 29
#define MIB_UPLOAD_NEXT_MAX_CONTENT_LEN 26


#define OMCI_BASELINE_CONT_LEN 32
#define OPTIONALMASKOFFSET 28
#define ERROREXEMASKOFFSET 30

#define MAX_ATTR_INDEX				255
#define OFFSET_OF_ATTR_FOR_GET_RESPONSE 3
#define OFFSET_OF_ATTR_FOR_EXTEND_GET_RESPONSE 7

#define ONU_G_EXTENDED_TC_LAYER_OPTIONS   13


//get info 
//uint8_t	*getAttrValuePtrByInst(uint16_t meClassId, uint16_t instanceId);
uint16_t getAttrStructLengthByClass(omciManageEntity_ptr mePtr);

//parse msg
int reparseAttrStructToOmciPayLoad(omciGenericPayload_t * omciPayLoad, uint8_t *attrStructPtr, uint16_t maxLength, uint16_t validMask);

int convertToOmciGenericMsg(omciGenericPayload_ptr genericPayloadPtr);
int convertToOmciBaslineMsg(omciGenericPayload_ptr genericPayloadPtr);

int32_t getTheValue(uint8_t* value, uint32_t tableSize);

int omciCreateAction(uint16_t meClassId , omciMeInst_t *meInstant_ptr, omciGenericPayload_ptr omciPayLoad, uint8_t msgSrc);

int omciDeleteAction(uint16_t meClassId , omciMeInst_t *meInstant_ptr, omciGenericPayload_ptr omciPayLoad, uint8_t msgSrc);

int omciSetAction(uint16_t meClassId , omciMeInst_t *meInstant_ptr, omciGenericPayload_ptr omciPayLoad, uint8_t msgSrc);
				

int omciGetAction(uint16_t meClassId , omciMeInst_t *meInstant_ptr, omciGenericPayload_ptr omciPayLoad, uint8_t msgSrc);
	
	
int omciGetAllAlarmsAction(uint16_t meClassId , omciMeInst_t *meInstant_ptr, omciGenericPayload_ptr omciPayLoad, uint8_t msgSrc);

int omciGetAllAlarmsNextAction(uint16_t meClassId , omciMeInst_t *meInstant_ptr, omciGenericPayload_ptr omciPayLoad, uint8_t msgSrc);


int omciMIBUploadAction(uint16_t meClassId , omciMeInst_t *meInstant_ptr, omciGenericPayload_ptr omciPayLoad, uint8_t msgSrc);
	
	
int omciMIBUploadNextAction(uint16_t meClassId , omciMeInst_t *meInstant_ptr, omciGenericPayload_ptr omciPayLoad, uint8_t msgSrc);

int omciMIBResetAction(uint16_t meClassId , omciMeInst_t *meInstant_ptr, omciGenericPayload_ptr omciPayLoad, uint8_t msgSrc);

int omciTestAction(uint16_t meClassId , omciMeInst_t *meInstant_ptr, omciGenericPayload_t * omciPayLoad, uint8_t msgSrc);

int omciSyncTimeAction(uint16_t meClassId , omciMeInst_t *meInstant_ptr, omciGenericPayload_ptr omciPayLoad, uint8_t msgSrc);

int omciRebootAction(uint16_t meClassId , omciMeInst_t *meInstant_ptr, omciGenericPayload_ptr omciPayLoad, uint8_t msgSrc);
int omciGetNextAction(uint16_t meClassId , omciMeInst_t *meInstant_ptr, omciGenericPayload_ptr omciPayLoad, uint8_t msgSrc);

int omciGetCurrentDataAction(uint16_t meClassId , omciMeInst_t *meInstant_ptr, omciGenericPayload_ptr omciPayLoad, uint8_t msgSrc);
int omciSetTableAction(uint16_t meClassId , omciMeInst_t *meInstant_ptr, omciGenericPayload_t * omciPayLoad, uint8_t msgSrc);

uint16_t omciMIBUploadCalculatePmMe(omciManageEntity_ptr mePtr, uint8_t *pMIBBuffer,uint32_t specFlag);
uint16_t omciMIBUploadCalculatePmMe_Extend(omciManageEntity_ptr mePtr, uint8_t *pMIBBuffer, uint32_t *pOffset);
uint16_t omciMIBUploadCalculateCommomMe(omciManageEntity_ptr mePtr, uint8_t *pMIBBuffer);
uint16_t omciMIBUploadCalculateCommomMe_Extend(omciManageEntity_ptr mePtr, uint8_t *pMIBBuffer, uint32_t *pOffset);

void responseMessageTypeHandle(uint8_t *mtPtr);
int writeResultAndReasonToOmciPayLoad(uint8_t result, uint8_t* responseContentPtr);
int omciDataSyncSeqIncret(void);
uint32_t compute_crc32_big_endian(char *path);
int omciHandshakeAVCResponseAction(uint16_t meClassId , omciMeInst_t *meInstant_ptr, omciGenericPayload_t * omciPayLoad, uint8_t msgSrc);
int getInstNum(uint16_t classId, uint16_t instStartId);

int32_t getGeneralTableValue(uint8_t *value, uint8_t *meInstantPtr, omciAttriDescript_ptr omciAttribute, uint8_t flag);
uint8_t getOmciDataSyncSeq(void);


uint8_t *omciGetAttriValueFromInst(uint16_t meClassId, uint16_t instanceId, uint8_t attrIndex);
int clearMeInstAndRule();
extern int (*omciActionStartSwDownload)(uint16_t meClassId , omciMeInst_t *meInstant_ptr, omciGenericPayload_t *omciPayLoad, uint8_t msgSrc);
extern int (*omciActionDownloadSec)(uint16_t meClassId , omciMeInst_t *meInstant_ptr, omciGenericPayload_t *omciPayLoad, uint8_t msgSrc);
extern int (*omciActionEndSwDownload)(uint16_t meClassId , omciMeInst_t *meInstant_ptr, omciGenericPayload_t *omciPayLoad, uint8_t msgSrc);
extern int (*omciActionActiveSw)(uint16_t meClassId , omciMeInst_t *meInstant_ptr, omciGenericPayload_t *omciPayLoad, uint8_t msgSrc);
extern int (*omciActionCommitSw)(uint16_t meClassId , omciMeInst_t *meInstant_ptr, omciGenericPayload_t *omciPayLoad, uint8_t msgSrc);

extern AttributeFunc generalGetFunc;
extern AttributeFunc generalGetSetFunc;
#endif
